#include "unistd.h"
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>

int runcommand(char *command)
{
    char *argv[5];
    int k = 0, i = 0;
    pid_t pid;

    printf("now pid = %d\n", getpid());

    while(*(command + i) != '\0'){
        while((*(command+i) != ' ') && (*(command + i) != '\0')){
            i++;
        }
        argv[k] = malloc(30);
        strncpy(argv[k++], command, i );
        command += (i + 1);
        i = 0;
    }
    argv[k] = NULL;

    if ( (pid = fork() ) < 0)
    {
        printf("fork error");
    }
    else if (pid == 0)
    {
        /* specify pathname, specify environment */
        //if (execv("/bin/busybox", command, (char *)0) < 0)
        if (execv("/bin/busybox", argv) < 0)
            printf("execl error");
    }

    printf("pid = %d\n", pid);
    if (waitpid(pid, NULL, 0) < 0){
        printf("error getpid = %d\n", getpid());
        printf("wait pid %d error\n", pid);
    }
    printf("pid = %d\n", pid);

}

int main(int argc, char *argv[])
{
    runcommand("ls -l -h");
    return 0;
}
